package com.hm.cms.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.cms.mongo.vo.CmsArticleContentVo;
import com.hm.cms.share.service.CmsArticleContentService;
import com.hm.cms.util.CmsConfigDef;
import com.hm.cms.web.su.BaseController;
import com.hm.common.annotation.AuthenticationAnnotation;
import com.hm.common.annotation.RequestPlanAnnotation;
import com.hm.common.common.CommonUtil;
import com.hm.common.model.ControllerResult;
import com.hm.common.model.PageInfo;
import com.hm.common.util.CommonDef.OperationStatus;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author shishun.wang
 * @date 下午2:41:22 2016年8月9日
 * @version 1.0
 * @describe
 */
@ResponseBody
@RestController
@RequestMapping(value = CmsConfigDef.Api.Rest.Cms.REST_CMS_ARTICLE_CONTENT)
public class CmsArticleContentController extends BaseController {

	public static final String swaggerTag = "内容管理";

	@Autowired
	private CmsArticleContentService cmsArticleContentService;

	@AuthenticationAnnotation
	@RequestMapping(value = "/{contentId}", method = RequestMethod.GET)
	@ApiOperation(value = "获取CMS内容管理", httpMethod = "GET", notes = "获取CMS站点内容管理通过ID", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取CMS内容管理", content = "获取CMS站点内容管理通过ID")
	public ResponseEntity<ControllerResult<CmsArticleContentVo>> getArticleContent(
			@ApiParam(required = true, name = "contentId", value = "内容管理ID") @PathVariable String contentId) {
		try {
			if (CommonUtil.isEmpty(contentId)) {
				return ControllerResult.failed(OperationStatus.LOAD_FAIL + ",内容管理ID不能为空");
			}
			return ControllerResult.success(cmsArticleContentService.getCmsArticleContent(contentId),
					OperationStatus.LOAD_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(value = "添加CMS站点内容管理", httpMethod = "POST", notes = "添加CMS站点内容管理", tags = swaggerTag)
	@RequestPlanAnnotation(title = "添加CMS站点内容管理", content = "添加CMS站点内容管理")
	public ResponseEntity<ControllerResult<String>> addCmsArticleContent(
			@ApiParam(required = true, name = "body", value = "站点内容管理") @RequestBody(required = true) CmsArticleContentVo articleContentVo) {
		try {
			if (CommonUtil.isEmpty(articleContentVo.getWeibsite(), articleContentVo.getColumn(),
					articleContentVo.getTitle(), articleContentVo.getContent())) {
				return ControllerResult.failed(OperationStatus.ADD_FAIL + ",基本信息不完整");
			}
			return ControllerResult.success(
					cmsArticleContentService.addCmsArticleContent(articleContentVo, currentUserId()),
					OperationStatus.ADD_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{contentId}", method = RequestMethod.PUT)
	@ApiOperation(value = "修改CMS内容管理", httpMethod = "PUT", notes = "修改CMS内容管理", tags = swaggerTag)
	@RequestPlanAnnotation(title = "修改CMS内容管理", content = "修改CMS内容管理")
	public ResponseEntity<ControllerResult<String>> updateCmsArticleContent(
			@ApiParam(required = true, name = "body", value = "内容管理信息") @RequestBody(required = true) CmsArticleContentVo articleContentVo,
			@ApiParam(required = true, name = "contentId", value = "内容管理ID") @PathVariable String contentId) {
		try {
			if (CommonUtil.isEmpty(articleContentVo.getWeibsite(), articleContentVo.getColumn(),
					articleContentVo.getTitle(), articleContentVo.getContent())) {
				return ControllerResult.failed(OperationStatus.UPDATE_FAIL + ",基本信息不完整");
			}
			articleContentVo.setId(contentId);
			return ControllerResult.success(
					cmsArticleContentService.updateCmsArticleContent(articleContentVo, currentUserId()),
					OperationStatus.UPDATE_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{contentId}", method = RequestMethod.DELETE)
	@ApiOperation(value = "删除CMS内容管理", httpMethod = "DELETE", notes = "删除CMS内容管理", tags = swaggerTag)
	@RequestPlanAnnotation(title = "删除CMS内容管理", content = "删除CMS内容管理")
	public ResponseEntity<ControllerResult<String>> deleteCmsArticleContent(
			@ApiParam(required = true, name = "contentId", value = "站点内容管理ID") @PathVariable String contentId) {
		try {
			if (CommonUtil.isEmpty(contentId)) {
				return ControllerResult.failed(OperationStatus.DELELETE_FAIL + ",站点内容管理ID不能为空");
			}
			CmsArticleContentVo articleContentVo = new CmsArticleContentVo();
			{
				articleContentVo.setId(contentId);
			}
			return ControllerResult.success(
					cmsArticleContentService.deleteCmsArticleContent(articleContentVo, currentUserId()),
					OperationStatus.DELELETE_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(value = "获取CMS内容管理数据", httpMethod = "GET", notes = "获取CMS内容管理数据", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取CMS内容管理数据", content = "获取CMS内容管理数据")
	public ResponseEntity<ControllerResult<PageInfo<CmsArticleContentVo>>> queryCmsArticleContent(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,小于1则默认返回第一页)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = true, name = "weibsite", value = "站点编号") @RequestParam(required = true) String weibsite,
			@ApiParam(required = true, name = "column", value = "站点栏目编号") @RequestParam(required = true) String column) {
		try {
			if (CommonUtil.isEmpty(pageNumber, pageSize)) {
				return ControllerResult.failed(OperationStatus.LOAD_FAIL + ",请求参数错误");
			}
			return ControllerResult.success(
					cmsArticleContentService.queryCmsArticleContent(weibsite, column, pageNumber, pageSize),
					OperationStatus.LOAD_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
}
